/*
 * @Author: wangshiyang
 * @Date: 2023-04-23 11:01:40
 * @LastEditors: WangShuai 1830773767@qq.com
 * @LastEditTime: 2025-05-21 18:52:10
 * @Description: 渐变墙体
 */
import * as Cesium from 'cesium'

function GrandientWallMaterialProperty(color) {
    this._definitionChanged = new Cesium.Event();
    this._color = undefined;
    this._colorSubscription = undefined;
    this.color = color;
}
Object.defineProperties(GrandientWallMaterialProperty.prototype, {
    isConstant: {
        get: function () {
            return false;
        }
    },
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        }
    },
    color: Cesium.createPropertyDescriptor('color')
});
GrandientWallMaterialProperty.prototype.getType = function (time) {
    return 'GrandientWall';
};
GrandientWallMaterialProperty.prototype.getValue = function (time, result) {
    if (!Cesium.defined(result)) {
        result = {};
    }
    result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
    result.image=Cesium.Material.GrandientWallImage
    return result;
};
GrandientWallMaterialProperty.prototype.equals = function (other) {
    return this === other ||
        (other instanceof GrandientWallMaterialProperty &&
            Cesium.Property.equals(this._color, other._color));
};

Cesium.Material.GrandientWallType = 'GrandientWall';
Cesium.Material.GrandientWallImage = 'src/assets/line.png';
Cesium.Material.GrandientWallSource =/*glsl*/`
    czm_material czm_getMaterial(czm_materialInput materialInput){
        czm_material material = czm_getDefaultMaterial(materialInput);
        vec2 st=materialInput.st;
        float ITime=czm_frameNumber/120.;
        // 因为我们line图片是横着的，所以st要反过来
        vec4 colorImage=texture2D(image, vec2(fract(st.t-ITime), st.s));
        material.alpha=colorImage.a*color.a;
        material.diffuse=color.rgb*2.;
        return material;
    }
`

Cesium.Material._materialCache.addMaterial(Cesium.Material.GrandientWallType, {
    fabric: {
        type: Cesium.Material.GrandientWallType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
            image: Cesium.Material.GrandientWallImage
        },
        source: Cesium.Material.GrandientWallSource
    },
    translucent: function (material) {
        return true;
    }
});

export default GrandientWallMaterialProperty